import { Public } from '@/decorators';
import { SettingService } from '@/services/setting.service';
import { ok } from '@/utils';
import { Body, Controller, Get, Param, Patch } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { BaseController } from './base.controller';

@Controller('setting')
export class SettingController extends BaseController {
  constructor(private readonly settingService: SettingService) {
    super();
  }

  @Public()
  @Get(':key')
  async getItem(@Param('key') key: string) {
    if (key === 'payment') {
      return ok({
        isUseBalance: this.config.get('isUseBalance'),
        payment: this.config.get('payment'),
      });
    }
    const setting = await this.settingService.getDefault();
    if (key === 'download') {
      return ok({
        iOSUrl: setting?.iOSUrl,
        androidUrl: setting?.androidUrl,
      });
    }
    return ok(setting[key]);
  }

  @Get('admin/info')
  async getInfo() {
    return ok(await this.settingService.getDefault());
  }

  @Patch('admin/update')
  async update(@Body() dto: Prisma.SettingUpdateInput) {
    return ok(await this.settingService.update(dto));
  }

  @Get('app/version')
  async getAppVersion() {
    return ok(await this.settingService.getAppVersion());
  }
}
